package com.example.administrator.u17.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.GameDetailActivity;
import com.example.administrator.u17.model.MineGameHOTRecomment;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/15.
 */

public class GameHeaderAdapter extends BaseAdapter{

    private ArrayList<MineGameHOTRecomment.DataBean.ReturnDataBean.GameheaderBean.RecommandsBean> mList;
    private Context context;

    public GameHeaderAdapter(ArrayList<MineGameHOTRecomment.DataBean.ReturnDataBean.GameheaderBean.RecommandsBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=View.inflate(context, R.layout.mine_header_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        Picasso.with(context).load(mList.get(position).getCoverUrl()).into(holder.imageView);
        float storage=mList.get(position).getSize()/1024;
        holder.tvNeiCun.setText(storage+"MB");
        holder.tvGameName.setText(mList.get(position).getTitle());
        holder.tvGamenType.setText(mList.get(position).getGameType());

        Toast.makeText(context, ""+mList.size(), Toast.LENGTH_SHORT).show();

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int appId=mList.get(position).getAppId();
                Intent intent=new Intent(context,GameDetailActivity.class);
                intent.putExtra("appId",appId);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    public static class  ViewHolder{
        private TextView tvGameName,tvGamenType,tvNeiCun;
        private ImageView imageView;

        public ViewHolder(View itemView) {
            tvGameName= (TextView) itemView.findViewById(R.id.tv_mine_game_Name);
            tvGamenType= (TextView) itemView.findViewById(R.id.tv_mine_game_describe_1);
            tvNeiCun= (TextView) itemView.findViewById(R.id.tv_mine_game_NeiCun_1);
            imageView= (ImageView) itemView.findViewById(R.id.iv_mine_game_1);
        }
    }
}
